package cn.minsh.lims.eoms.modules.app.utils;

import cn.minsh.lims.eoms.common.utils.DateUtils;
import com.alibaba.druid.util.DaemonThreadFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 */
public class LocalDateUtils extends DateUtils{

    protected static String[] WEEKS = {"星期一","星期二","星期三","星期四","星期五","星期六","星期日"} ;


    public static String getTimeStr(Date startTime, Date endTime){
        String startTimeStr = format(startTime,DATE_PATTERN) ;
        String endTimeStr = format(endTime,DATE_PATTERN) ;
        //判断是否同一天
        if (!startTimeStr.equals(endTimeStr)){  //非同一天
            return format(startTime,DIAGONAL_DATE_TIME_PATTERN)+"-"+format(endTime,DIAGONAL_DATE_TIME_PATTERN) ;
        }else{  //同一天
            return format(startTime,DIAGONAL_DATE_TIME_PATTERN)+"-"+format(endTime,HOUR_MINUTE) ;
        }
    }

    public static Integer getDayCount(Date today,Date firstDate) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat(DATE_PATTERN);
        Date first= df.parse(format(today,DATE_PATTERN)) ;
        Date second = df.parse(format(firstDate,DATE_PATTERN)) ;
        return  (int)((first.getTime()-second.getTime())/(24*60*60*1000));
    }

    public static Integer getWeekOfDate(Date dt) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);

        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) {
            w = 0;
        }
        return w;
    }

    public static String getWeekNameOfDate(Date dt){
        Integer index = getWeekOfDate(dt) ;
        if (index ==0){
            index = 7 ;
        }
        return WEEKS[index-1] ;
    }

    public static Integer getDayOfMonth(Date dt){
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        return cal.get(Calendar.DAY_OF_MONTH) ;
    }

    public static Integer getMonthDayOfYear(Date dt){
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        return cal.get(Calendar.DAY_OF_YEAR) ;
    }



    public static int compareDay(Date firstDate, Date secondDate) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(firstDate);
        int day1 = cal.get(Calendar.DAY_OF_YEAR) ;
        cal.setTime(secondDate);
        int day2 = cal.get(Calendar.DAY_OF_YEAR) ;
        return day1-day2 ;
    }

    public static int compareWeek(Date firstDate, Date secondDate) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(firstDate);
        int week1 = cal.get(Calendar.DAY_OF_WEEK) ;
        cal.setTime(secondDate);
        int week2 = cal.get(Calendar.DAY_OF_WEEK) ;
        return week1-week2 ;
    }

    public static int compareMonthDay(Date firstDate, Date secondDate) {

        Calendar cal = Calendar.getInstance();
        cal.setTime(firstDate);
        int dayOfMonth1 = cal.get(Calendar.DAY_OF_MONTH) ;
        cal.setTime(secondDate);
        int dayOfMonth2 = cal.get(Calendar.DAY_OF_MONTH) ;
        return dayOfMonth1-dayOfMonth2 ;
    }

    public static int compareYearDay(Date firstDate, Date secondDate) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(firstDate);
        int day1 = cal.get(Calendar.DAY_OF_YEAR) ;
        cal.setTime(secondDate);
        int day2 = cal.get(Calendar.DAY_OF_YEAR) ;
        return day1-day2 ;
    }

    public static String getWeekTimeStr(Date startTime, Date endTime) {
        String startTimeStr = format(startTime,DATE_PATTERN) ;
        String endTimeStr = format(endTime,DATE_PATTERN) ;
        //判断是否同一天
        if (!startTimeStr.equals(endTimeStr)){  //非同一天

            String resStartTimeStr = format(startTime,DIAGONAL_DATE_TIME_PATTERN) ;
            String startWeekStr = getWeekNameOfDate(startTime) ;
            String resEndTimeStr = format(endTime,DIAGONAL_DATE_TIME_PATTERN) ;
            String endWeekStr = getWeekNameOfDate(endTime) ;
            resStartTimeStr = resStartTimeStr.replace(" ","("+startWeekStr+")") ;
            resEndTimeStr = resEndTimeStr.replace(" ","("+endWeekStr+")") ;
            return resStartTimeStr+"-"+resEndTimeStr;
        }else{  //同一天
            String weekStr = getWeekNameOfDate(startTime) ;
            String resStartTimeStr = format(startTime,DIAGONAL_DATE_TIME_PATTERN) ;
            resStartTimeStr = resStartTimeStr.replace(" ","("+weekStr+")") ;
            return resStartTimeStr+"-"+format(endTime,HOUR_MINUTE) ;
        }
    }

    public static String getQueueTimeStr(Date startTime,Date endTime){
        String startDate = format(startTime,DATE_PATTERN) ;
        String endDate = format(endTime,DATE_PATTERN) ;
        String endDateStr= null ;
        if(startDate.equals(endDate)){
            //同一天
            endDateStr = format(endTime,HOUR_MINUTE) ;
        }else{
            endDateStr = format(endTime,DATE_TIME_MINUTE_PATTERN) ;
        }

        return format(startTime,DATE_TIME_MINUTE_PATTERN)+"-"+endDateStr ;
    }


    public static Date getNextYear(Date firstPlanDate) {

        Calendar cal = Calendar.getInstance();
        cal.setTime(firstPlanDate);
        Integer month = cal.get(Calendar.MONTH) ;
        Integer day = cal.get(Calendar.DAY_OF_MONTH) ;
        System.out.println(cal.getTime());
        Calendar newCal = Calendar.getInstance() ;
        newCal.set(Calendar.MONTH,month);
        newCal.set(Calendar.DAY_OF_MONTH,day);
        System.out.println(newCal.getTime());
        return newCal.getTime() ;
    }


    public static Date getNextMonth(Date firstPlanDate) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(firstPlanDate);
        Integer day = cal.get(Calendar.DAY_OF_MONTH) ;
        System.out.println(cal.getTime());
        Calendar newCal = Calendar.getInstance() ;
        newCal.set(Calendar.DAY_OF_MONTH,day);
        System.out.println(newCal.getTime());
        return newCal.getTime() ;
    }

    public static Date getNextWeek(Date firstPlanDate) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(firstPlanDate);
        Integer oldWeek = cal.get(Calendar.DAY_OF_WEEK) ;
        System.out.println(cal.getTime());
        Calendar newCal = Calendar.getInstance() ;
        Integer todayWeek = newCal.get(Calendar.DAY_OF_WEEK) ;
        Integer addDays = 0 ;
        if (todayWeek>oldWeek){
            addDays = 7- (todayWeek-oldWeek) ;
        }else{
            addDays = oldWeek-todayWeek ;
        }

        Date newDate = addDateDays(newCal.getTime(),addDays) ;
        newCal.setTime(newDate);
        System.out.println(newCal);
        return newCal.getTime() ;
    }

    //判断是统一时间点，并且未工作日：周一到周五
    public static Boolean checkSameDayAndWeekdays(Date firstDay,Date secondDay){

        Calendar cal = Calendar.getInstance();
        cal.setTime(firstDay);
        int week1 = cal.get(Calendar.DAY_OF_WEEK) ;
        cal.setTime(secondDay);
        int week2 = cal.get(Calendar.DAY_OF_WEEK) ;
        //判断同一天
        if (week1 != week2){
            return false ;
        }
        if (week2 == 1 || week2 == 7){
            return false;
        }

        return true ;
    }

    public static void main(String[] args) throws ParseException {
        //Date newDate = addDateDays(new Date(),2) ;

        //System.out.println(getNextWeek(newDate));

        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        System.out.println(cal.getTime());
        int week1 = cal.get(Calendar.DAY_OF_WEEK) ;
        System.out.println(week1);
        cal.set(Calendar.DAY_OF_MONTH,cal.get(Calendar.DAY_OF_MONTH)-1);
        System.out.println(cal.getTime());
        System.out.println(cal.get(Calendar.DAY_OF_WEEK));
    }
}
